package netGaming;

import java.io.Serializable;
import java.util.Date;

import gameBasicElements.*;
import gameEngine.GameInfo;
import gameEngine.GameStatus;

/**
 * @author Wojciech Łopata
 *
 */

public class TransportableOneTurnGameInfo implements GameInfo, Serializable
{
	private static final long serialVersionUID = -4025036384946806793L;
	
	private Board currentBoard;
	private boolean gameStarted, gameEnded;
	private long blackPlayerTime, whitePlayerTime;
	private long turnStart;
	private PlayerColor currentPlayer;
	private String failInfo;
	private GameStatus status;
	private ScoreBoard finalScoreBoard;
	
	public TransportableOneTurnGameInfo(GameInfo infoSource)
	{
		currentBoard = infoSource.getCurrentBoard();
		gameStarted = infoSource.gameStarted();
		gameEnded = infoSource.gameFinished();
		blackPlayerTime = infoSource.getTimeLeft(PlayerColor.BLACK);
		whitePlayerTime = infoSource.getTimeLeft(PlayerColor.WHITE);
		turnStart = new Date().getTime();
		currentPlayer = infoSource.getCurrentPlayer();
		failInfo = infoSource.lastMoveError();
		status = infoSource.getGameStatus();
		finalScoreBoard = infoSource.getFinalScores();
	}

	@Override
	public Board getCurrentBoard()
	{
		return currentBoard;
	}

	@Override
	public PlayerColor getCurrentPlayer()
	{
		return currentPlayer;
	}

	@Override
	public String lastMoveError()
	{
		return failInfo;
	}

	@Override
	public boolean gameFinished()
	{
		return gameEnded;
	}

	@Override
	public boolean gameStarted()
	{
		return gameStarted;
	}

	@Override
	public int getTimeLeft(PlayerColor color)
	{
		return (int)((color == PlayerColor.WHITE ? whitePlayerTime : blackPlayerTime)
				- (color == currentPlayer ? Math.max(0, new Date().getTime() - turnStart) : 0));
	}
	
	@Override
	public GameStatus getGameStatus()
	{
		return status;
	}

	@Override
	public ScoreBoard getFinalScores()
	{
		return finalScoreBoard;
	}
}
